extends Node

@onready var sfx :Node = $SFX
@onready var bgm :AudioStreamPlayer = $BGMPlayer

enum Bus{
	Master,
	Music,
	Effect
}

#用与播放音效， 需要提前设置好
# 音效函数需要特殊加载 后面设计 todo
func play_sfx(name:String) -> void:
	var player = sfx.get_node(name) as AudioStreamPlayer
	if player == null :
		return
	
	player.play()	
	



# 在其他节点中直接调用此函数 即可 对按键的提示音 需要在脚本中传入节点
# 例子 setup_ui_sounds（self）
func setup_ui_sounds(node:Node)->void:
	var button :Button = node as Button
	if  button :
		button.pressed.connect(play_sfx.bind("UIPress"))
		button.focus_entered.connect(play_sfx.bind("UIFocus"))
	for child in node.get_children():
		setup_ui_sounds(child)
		
		
# 播放背背景音乐的函数
func _play_BGM(bgms:String) -> void:
	var dz:String = "res://Files/Music/mapBGM/"
	var stream = load(dz+bgms)
	if bgm.stream == stream and bgm.playing:
		return
	bgm.stream = stream
	bgm.play()		
func play_bgm(stream :AudioStream) -> void:
	
	if bgm.stream == stream and bgm.playing:
		return
	bgm.stream = stream
	bgm.play()		



# 音量总线的设置 ， 设置界面使用的函数
func get_volume(bus_index:int) -> float:
	var index := AudioServer.get_bus_volume_db(bus_index)
	return db_to_linear(index) 
	
func set_volume(bus_index:int , v :int) -> void :
	var value := linear_to_db(v)
	AudioServer.set_bus_volume_db(bus_index,value)
